import {
    getActivityList,
    signInActivity
} from '../../utils/request/index/indexReq'
import Toast from 'tdesign-miniprogram/toast/index';
import {
    parseTime
} from '../../utils/time.js'
// index.js

Page({
    data: {
        motto: 'Hello World',
        swiperList: [],
        activityList: [],
        pager: {
            pageSize: 6,
            currentPage: 1
        },
        activitySearchForm:{
            title: ''
        }
    },
    onShow() {
        console.log('进入主页')
        this.getTabBar().init();
        this.data.pager.currentPage = 1

        if (this.gotoLogin()) {
            return
        } else {
            if (this.data.activityList.length === 0) {
                this.setData({
                    activityList: [],
                    swiperList: []
                })
                this.getActivityList()
                this.getSwiperList()
            }
        }
    },
    onLoad() {

        // wx.navigateTo({
        //   url: '../manage/activityManage/activityManage',
        // })
        // wx.switchTab({
        //   url: '../manage/manage',
        // })


    },
    // 判断是否登录
    gotoLogin() {
        const token = wx.getStorageSync('token')
        console.log('正在获取token')
        console.log(token)
        if (token === null || token === undefined || token === '') {
            wx.navigateTo({
                url: '../login/login',
            })
            return true
        } else {
            return false
        }
    },
    // 获取活动列表
    getActivityList() {
        const query = {
            pager: {
                pageSize: this.data.pager.pageSize,
                currentPage: this.data.pager.currentPage
            },
            searchObject: {
                satateId: 1,
                title: this.data.activitySearchForm.title
            }
        }
        getActivityList(query).then(res => {
            if (res.statusCode === 200 && res.data.length > 0) {
                res.data.forEach(item => {
                    item.activityStart = parseTime(item.activityStart, '{y}-{m}-{d}')
                })
                this.setData({
                    activityList: this.data.activityList.concat(res.data)
                })
            }
        })
    },
    // 获取轮播图数据
    getSwiperList() {
        const query = {
            pager: {
                pageSize: 5,
                currentPage: 1
            },
            searchObject: {
                stateId: 1
            }
        }
        // 获取活动数据
        getActivityList(query).then(res => {
            if (res.statusCode === 200 && res.data.length > 0) {
                    res.data.forEach(item => {
                        item.activityStart = parseTime(item.activityStart, '{y}-{m}-{d}')
                    })
                    this.setData({
                        swiperList: res.data
                    })
            }
        })
    },
    // 跳转活动详情页面
    gotoActivityDetail(row) {
        wx.setStorageSync('activityDetailId', row.currentTarget.dataset.activityid);
        wx.navigateTo({
            url: '/pages/activityDetail/activityDetail',
        })
    },
    // 启动扫码登录
    handleScanCode() {
        let _this = this
        wx.scanCode({
            onlyFromCamera: true,
            success(res) {
                const scanCode = res.result
                // 签到接口
                signInActivity(scanCode).then(resp => {
                    if(resp.data.code === 200){
                        Toast({
                            context: _this,
                            selector: '#t-toast',
                            message: '签到成功',
                            icon: 'check-circle',
                            direction: 'column',
                          });
                    }else {
                        Toast({
                            context: _this,
                            selector: '#t-toast',
                            message: resp.data.message,
                            icon: 'uncomfortable',
                            direction: 'column',
                          });
                    }
                })
            }
        })
    },
    handleTitleInput(event){
        const value = event.detail.value
        this.setData({
            'activitySearchForm.title': value,
            activityList: [],
            'pager.currentPage': 1,
        })
        this.getActivityList()
    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this.setData({
            pager: {
                pageSize: this.data.pager.pageSize,
                currentPage: this.data.pager.currentPage + 1
            }
        })
        this.getActivityList()
    },
    methods: {

    },
})